package cn.exrick.xboot.modules.account.serviceimpl;

import cn.exrick.xboot.modules.account.entity.Account;
import cn.exrick.xboot.modules.account.mapper.AccountMapper;
import cn.exrick.xboot.modules.account.service.AccountService;

import cn.exrick.xboot.modules.projectinfo.entity.NumberAndMoneyTwice;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.ULongLongSeqHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@DS("other")
@Transactional
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    /**
     * 获取大屏数据本年
     */
    @Override
    public Account getAccountByYear() {
//       获取现金流入
        Account account = accountMapper.getxjlr();
//      获取现金流出
        Account account1 = accountMapper.getxjlc();
//     获取收入确认金额
        Account account2 = accountMapper.getsrqr();
//        获取应收账款金额
        Account account3 = accountMapper.getyszk();
//        获取开票金额
        Account account4 = accountMapper.getkpmoney();
//        获取收票金额
        Account account5 = accountMapper.getspmoney();
//        判断填充数据--现金流出
        if (account1 != null) {
            account.setXjlc(account1.getXjlc());
        }
//        判断填充数据--收入确认金额
        if (account2 != null) {
            account.setSrqr(account2.getSrqr());
        }
//        判断填充数据--应收账款金额
        if (account3 != null) {
            account.setYszk(account3.getYszk());
        }
//        判断填充数据--开票金额
        if (account4 != null) {
            account.setKpMoney(account4.getKpMoney());
        }
//        判断填充数据--收票金额
        if (account5 != null) {
            account.setSpMoney(account5.getSpMoney());
        }
        return account;
    }


    /**
     * 获取大屏数据本月
     */
    @Override
    public Account getAccountByMonth() {
//       获取现金流入
        Account account = accountMapper.getxjlrByMonth();
//      获取现金流出
        Account account1 = accountMapper.getxjlcByMonth();
//     获取收入确认金额
        Account account2 = accountMapper.getsrqrByMonth();
//        获取应收账款金额
        Account account3 = accountMapper.getyszkByMonth();
//        获取开票金额
        Account account4 = accountMapper.getkpmoneyByMonth();
//        获取收票金额
        Account account5 = accountMapper.getspmoneyByMonth();
//        判断填充数据--现金流出
        if (account1 != null) {
            account.setXjlc(account1.getXjlc());
        }
//        判断填充数据--收入确认金额
        if (account2 != null) {
            account.setSrqr(account2.getSrqr());
        }
//        判断填充数据--应收账款金额
        if (account3 != null) {
            account.setYszk(account3.getYszk());
        }
//        判断填充数据--开票金额
        if (account4 != null) {
            account.setKpMoney(account4.getKpMoney());
        }
//        判断填充数据--收票金额
        if (account5 != null) {
            account.setSpMoney(account5.getSpMoney());
        }
        return account;
    }


    /**
     * 现金流排行--本年
     *
     * @return
     */
    @Override
    public List<Account> getXJLROrderByYear() {
        List<Account> account = accountMapper.getXJLROrderByYear();
        List<Account> account2 = accountMapper.getXJLCOrderByYear();
        for (Account account1 : account) {
            for (Account account3 : account2) {
//                填充现金流出
                if (account3.getDeptName().equals(account1.getDeptName())) {
                    account1.setXjlc(account3.getXjlc());
                }
            }
            if (account1.getXjlr() == null) {
                account1.setXjlr(0L);
            }
            if (account1.getXjlc() == null) {
                account1.setXjlc(0L);
            }
        }
        return account;
    }

    /**
     * 现金流流出排行--本年
     *
     * @return
     */
    @Override
    public List<Account> getXJLCOrderByYear() {
        List<Account> account = accountMapper.getXJLROrderByYear();
        return account;
    }


    /**
     * 现金流流入排行--本月
     *
     * @return
     */
    @Override
    public List<Account> getXJLROrderByMonth() {
        List<Account> account = accountMapper.getXJLROrderByMonth();
        List<Account> account2 = accountMapper.getXJLCOrderByMonth();
        for (Account account1 : account) {
            for (Account account3 : account2) {
//                填充现金流出
                if (account3.getDeptName().equals(account1.getDeptName())) {
                    account1.setXjlc(account3.getXjlc());
                }
            }
            if (account1.getXjlr() == null) {
                account1.setXjlr(0L);
            }
            if (account1.getXjlc() == null) {
                account1.setXjlc(0L);
            }
        }
        return account;
    }


    /**
     * 应收账款排行--本年
     *
     * @return
     */
    @Override
    public List<Account> getYSZKOrder() {
//        获取结算款
        List<Account> account = accountMapper.getYSZKOrder();
//        获取进度款
        List<Account> account1 = accountMapper.getYSZKOrderjdk();
//        填充数据
        if (account != null && account1 != null) {
            for (Account account2 : account) {
                for (Account account3 : account1) {
                    if (account3.getDeptName().equals(account2.getDeptName())) {
                        account2.setYszkjdk(account3.getYszkjdk());
                    }
                }
                if (account2.getYszkjdk() == null) {
                    account2.setYszkjdk(0L);
                }
                if (account2.getYszkjsk() == null) {
                    account2.setYszkjsk(0L);
                }
            }
        }
        return account;
    }


    /**
     * 应收账款排行--本月
     *
     * @return
     */
    @Override
    public List<Account> getYSZKOrderByMonth() {
//        获取结算款
        List<Account> account = accountMapper.getYSZKOrderByMonth();
//        获取进度款
        List<Account> account1 = accountMapper.getYSZKOrderjdkByMonth();
//        填充数据
        if (account != null && account1 != null) {
            for (Account account2 : account) {
                for (Account account3 : account1) {
                    if (account3.getDeptName().equals(account2.getDeptName())) {
                        account2.setYszkjdk(account3.getYszkjdk());
                    }
                }
                if (account2.getYszkjdk() == null) {
                    account2.setYszkjdk(0L);
                }
                if (account2.getYszkjsk() == null) {
                    account2.setYszkjsk(0L);
                }
            }
        }
        return account;
    }

    /**
     * 收入确认金额排行--本年
     *
     * @return
     */
    @Override
    public List<Account> getSRQROrder() {
        List<Account> list = accountMapper.getSRQROrder();
        if (list != null) {
            return list;
        }
        return null;
    }

    /**
     * 收入确认金额排行--本月
     *
     * @return
     */
    @Override
    public List<Account> getSRQROrderByMonth() {
        List<Account> list = accountMapper.getSRQROrderByMonth();
        if (list != null) {
            return list;
        }
        return null;
    }

    /**
     * 合同签订金额--本年
     *
     * @return
     */
    @Override
    public List<Account> getContractMoney() {
        List<Account> list = accountMapper.getContractMoney();
        return list;
    }

    /**
     * 合同签订金额--本月
     *
     * @return
     */
    @Override
    public List<Account> getContractMoneyByMonth() {
        List<Account> list = accountMapper.getContractMoneyByMonth();
        return list;
    }


    /**
     * 现金流情况柱状图--本年
     *
     * @return
     */
    @Override
    public LinkedList<Object> getXJLLine() {
//        创建返回集合
        LinkedList<Object> list6 = new LinkedList<>();
//        创建三个集合
        LinkedList<Object> list3 = new LinkedList<>();
        LinkedList<Object> list4 = new LinkedList<>();
        LinkedList<Object> list5 = new LinkedList<>();

//        得到现金流入单位，金额
        List<Account> list1 = accountMapper.getXJLROrderByYear();
//        得到现金流出单位，金额
        List<Account> list2 = accountMapper.getXJLCOrderByYear();
        if (list1 != null && list2 != null) {
            for (Account account : list1) {
                for (Account account1 : list2) {
                    if (account1.getDeptName().equals(account.getDeptName())) {
                        account.setXjlc(account1.getXjlc());
                    }
                }
                if (account.getXjlr() == null) {
                    account.setXjlr(0L);
                }
                if (account.getXjlc() == null) {
                    account.setXjlc(0L);
                }
            }
        }
        for (Account account : list1) {
            list3.add(account.getDeptName());
            list4.add(account.getXjlr());
            list5.add(account.getXjlc());
        }
        Object[] objects = list3.toArray();
        Object[] objects1 = list4.toArray();
        Object[] objects2 = list5.toArray();
        list6.add(objects);
        list6.add(objects1);
        list6.add(objects2);
        return list6;
    }

    /**
     * 现金流情况柱状图--本月
     *
     * @return
     */
    @Override
    public LinkedList<Object> getXJLLineByMonth() {
//        创建返回集合
        LinkedList<Object> list6 = new LinkedList<>();
//        创建三个集合
        LinkedList<Object> list3 = new LinkedList<>();
        LinkedList<Object> list4 = new LinkedList<>();
        LinkedList<Object> list5 = new LinkedList<>();

//        得到现金流入单位，金额
        List<Account> list1 = accountMapper.getXJLROrderByMonth();
//        得到现金流出单位，金额
        List<Account> list2 = accountMapper.getXJLCOrderByMonth();
        if (list1 != null && list2 != null) {
            for (Account account : list1) {
                for (Account account1 : list2) {
                    if (account1.getDeptName().equals(account.getDeptName())) {
                        account.setXjlc(account1.getXjlc());
                    }
                }
                if (account.getXjlr() == null) {
                    account.setXjlr(0L);
                }
                if (account.getXjlc() == null) {
                    account.setXjlc(0L);
                }
            }
        }
        for (Account account : list1) {
            list3.add(account.getDeptName());
            list4.add(account.getXjlr());
            list5.add(account.getXjlc());
        }
        Object[] objects = list3.toArray();
        Object[] objects1 = list4.toArray();
        Object[] objects2 = list5.toArray();
        list6.add(objects);
        list6.add(objects1);
        list6.add(objects2);
        return list6;
    }


    /**
     * 合同签订情况--柱状折线图--本年
     *
     * @return
     */
    @Override
    public LinkedList<Object> getContractInfoLine() {
        //        创建返回集合
        LinkedList<Object> list6 = new LinkedList<>();
//        创建三个集合
        LinkedList<Object> list3 = new LinkedList<>();
        LinkedList<Object> list4 = new LinkedList<>();
        LinkedList<Object> list5 = new LinkedList<>();
        List<Account> list = accountMapper.getContractInfoLine();
        for (Account account : list) {
            list3.add(account.getDeptName());
            list4.add(account.getHtqdNumber());
            list5.add(account.getHtqdMoney());
        }
        list6.add(list3.toArray());
        list6.add(list4.toArray());
        list6.add(list5.toArray());
        return list6;
    }

    /**
     * 合同签订情况--柱状折线图--本月
     *
     * @return
     */
    @Override
    public LinkedList<Object> getContractInfoLineByMonth() {
        //        创建返回集合
        LinkedList<Object> list6 = new LinkedList<>();
//        创建三个集合
        LinkedList<Object> list3 = new LinkedList<>();
        LinkedList<Object> list4 = new LinkedList<>();
        LinkedList<Object> list5 = new LinkedList<>();
        List<Account> list = accountMapper.getContractInfoLineByMonth();
        for (Account account : list) {
            list3.add(account.getDeptName());
            list4.add(account.getHtqdNumber());
            list5.add(account.getHtqdMoney());
        }
        list6.add(list3.toArray());
        list6.add(list4.toArray());
        list6.add(list5.toArray());
        return list6;
    }

    /**
     * 收票开票情况--本年
     *
     * @return
     */
    @Override
    public List<Account> getSPKPOrder() {
        List<Account> list = accountMapper.getSPOrder();
        List<Account> list1 = accountMapper.getKPOrder();
        if (list != null && list1 != null) {
            for (Account account : list) {
                for (Account account1 : list1) {
                    if (account1.getDeptName().equals(account.getDeptName())) {
                        account.setKpMoney(account1.getKpMoney());
                    }
                }
                if (account.getSpMoney() == null) {
                    account.setSpMoney(0L);
                }
                if (account.getKpMoney() == null) {
                    account.setKpMoney(0L);
                }
            }
        }
        return list;
    }

    /**
     * 收票开票情况--本月
     *
     * @return
     */
    @Override
    public List<Account> getSPKPOrderByMonth() {
        List<Account> list = accountMapper.getSPOrderByMonth();
        List<Account> list1 = accountMapper.getKPOrderByMonth();
        if (list != null && list1 != null) {
            for (Account account : list) {
                for (Account account1 : list1) {
                    if (account1.getDeptName().equals(account.getDeptName())) {
                        account.setKpMoney(account1.getKpMoney());
                    }
                }
                if (account.getSpMoney() == null) {
                    account.setSpMoney(0L);
                }
                if (account.getKpMoney() == null) {
                    account.setKpMoney(0L);
                }
            }
        }
        return list;
    }


    /**
     * 现金流情况--折线图-按照时间
     *
     * @return
     */
    @Override
    public LinkedList<Object> getXJLInfoByTime() {
        //        创建返回集合
        LinkedList<Object> list6 = new LinkedList<>();
//        创建三个集合
        LinkedList<Object> list3 = new LinkedList<>();
        LinkedList<Object> list4 = new LinkedList<>();
        LinkedList<Object> list5 = new LinkedList<>();
//        获取现金流入情况
        List<Account> list = accountMapper.getXJLInfoByTime();
        for (Account account : list) {
            account.setYear(account.getYear() + "-" + account.getMonth());
        }
//        获取现金流出情况
        List<Account> list1 = accountMapper.getXJLCInfoByTime();
        for (Account account : list1) {
            account.setYear(account.getYear() + "-" + account.getMonth());
        }
//        填充数据
        if (list != null) {
            for (Account account : list) {
                for (Account account1 : list1) {
                    if (account1.getYear().equals(account.getYear())) {
                        account.setXjlc(account1.getXjlc());
                    }
                }
                if (account.getXjlr() == null) {
                    account.setXjlr(0L);
                }
                if (account.getXjlc() == null) {
                    account.setXjlc(0L);
                }
                list3.add(account.getYear());
                list4.add(account.getXjlr());
                list5.add(account.getXjlc());
            }
            list6.add(list3.toArray());
            list6.add(list4.toArray());
            list6.add(list5.toArray());
        }
        return list6;
    }


    /**
     * 获取单位情况--二级菜单--本年
     *
     * @return
     */
    @Override
    public List<Account> getDeptInfo(Account account6) {
//        获取所有单位名称
        List<Account> list = accountMapper.getAllDept();
//        获取合同签订金额
        List<Account> list1 = accountMapper.getContractMoney1();
//        获取收入确认金额
        List<Account> list2 = accountMapper.getSRQROrder1();
//        获取现金流入金额
        List<Account> list3 = accountMapper.getXJLROrderByYear1();
//        获取现金流出金额
        List<Account> list4 = accountMapper.getXJLCOrderByYear1();
//        获取净现金流金额
//        获取全年开票金额
        List<Account> list5 = accountMapper.getKPOrder1();
//        获取全年收票金额
        List<Account> list6 = accountMapper.getSPOrder1();
//        获取应收账款结算款金额
        List<Account> list7 = accountMapper.getYSZKOrder1();
//        获取应收账款进度款金额
        List<Account> list8 = accountMapper.getYSZKOrderjdk1();
//        封装数据
        if (list!=null){
            for (Account account : list) {
//                填充合同签订金额
                if (list1!=null){
                    for (Account account1 : list1) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setHtqdMoney(account1.getHtqdMoney());
                        }
                    }
                }
//                填充收入确认金额
                if (list2!=null){
                    for (Account account1 : list2) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setSrqr(account1.getSrqr());
                        }
                    }
                }
//                填充现金流入金额
                if (list3!=null){
                    for (Account account1 : list3) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setXjlr(account1.getXjlr());
                        }
                    }
                }
//                填充现金流出金额
                if (list4!=null){
                    for (Account account1 : list4) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setXjlc(account1.getXjlc());
                        }
                    }
                }
//                填充全年开票金额
                if (list5!=null){
                    for (Account account1 : list5) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setKpMoney(account1.getKpMoney());
                        }
                    }
                }
//                填充全年收票金额
                if (list6!=null){
                    for (Account account1 : list6) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setSpMoney(account1.getSpMoney());
                        }
                    }
                }
//                填充应收账款结算款金额
                if (list7!=null){
                    for (Account account1 : list7) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setYszkjsk(account1.getYszkjsk());
                        }
                    }
                }
//                填充应收账款进度款金额
                if (list8!=null){
                    for (Account account1 : list8) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setYszkjdk(account1.getYszkjdk());
                        }
                    }
                }
//                填充净现金流
                account.setJxjl(account.getXjlr()-account.getXjlc());
            }
        }
        String type = account6.getType();
        if (type.equals("1")){
            List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getXjlr).reversed())
                    .collect(Collectors.toList());
            return collect;
        }
        if (type.equals("2")){
            List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getXjlc).reversed())
                    .collect(Collectors.toList());
            return collect;
        }
        if (type.equals("3")){
            List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getJxjl).reversed())
                    .collect(Collectors.toList());
            return collect;
        }
        if (type.equals("4")){
            List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getSrqr).reversed())
                    .collect(Collectors.toList());
            return collect;
        }
        if (type.equals("5")){
            List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getYszkjsk).reversed())
                    .collect(Collectors.toList());
            return collect;
        }
        if (type.equals("6")){
            List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getKpMoney).reversed())
                    .collect(Collectors.toList());
            return collect;
        }
        if (type.equals("7")){
            List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getHtqdMoney).reversed())
                    .collect(Collectors.toList());
            return collect;
        }
        return list;
    }

    /**
     * 获取单位情况--二级菜单--本月
     * @return
     */
    @Override
    public List<Account> getDeptInfoByMonth(Account account6) {
        //        获取所有单位名称
        List<Account> list = accountMapper.getAllDept();
//        获取合同签订金额
        List<Account> list1 = accountMapper.getContractMoneyByMonth1();
//        获取收入确认金额
        List<Account> list2 = accountMapper.getSRQROrderByMonth1();
//        获取现金流入金额
        List<Account> list3 = accountMapper.getXJLROrderByMonth1();
//        获取现金流出金额
        List<Account> list4 = accountMapper.getXJLCOrderByMonth1();
//        获取净现金流金额
//        获取开票金额
        List<Account> list5 = accountMapper.getKPOrderByMonth1();
//        获取收票金额
        List<Account> list6 = accountMapper.getSPOrderByMonth1();
//        获取应收账款结算款金额
        List<Account> list7 = accountMapper.getYSZKOrderByMonth1();
//        获取应收账款进度款金额
        List<Account> list8 = accountMapper.getYSZKOrderjdkByMonth1();
//        封装数据
        if (list!=null){
            for (Account account : list) {
//                填充合同签订金额
                if (list1!=null){
                    for (Account account1 : list1) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setHtqdMoney(account1.getHtqdMoney());
                        }
                    }
                }
//                填充收入确认金额
                if (list2!=null){
                    for (Account account1 : list2) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setSrqr(account1.getSrqr());
                        }
                    }
                }
//                填充现金流入金额
                if (list3!=null){
                    for (Account account1 : list3) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setXjlr(account1.getXjlr());
                        }
                    }
                }
//                填充现金流出金额
                if (list4!=null){
                    for (Account account1 : list4) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setXjlc(account1.getXjlc());
                        }
                    }
                }
//                填充全年开票金额
                if (list5!=null){
                    for (Account account1 : list5) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setKpMoney(account1.getKpMoney());
                        }
                    }
                }
//                填充全年收票金额
                if (list6!=null){
                    for (Account account1 : list6) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setSpMoney(account1.getSpMoney());
                        }
                    }
                }
//                填充应收账款结算款金额
                if (list7!=null){
                    for (Account account1 : list7) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setYszkjsk(account1.getYszkjsk());
                        }
                    }
                }
//                填充应收账款进度款金额
                if (list8!=null){
                    for (Account account1 : list8) {
                        if (account1.getDeptName().equals(account.getDeptName())){
                            account.setYszkjdk(account1.getYszkjdk());
                        }
                    }
                }
//                填充净现金流
                account.setJxjl(account.getXjlr()-account.getXjlc());
            }
            String type = account6.getType();
            if (type.equals("1")){
                List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getXjlr).reversed())
                        .collect(Collectors.toList());
                return collect;
            }
            if (type.equals("2")){
                List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getXjlc).reversed())
                        .collect(Collectors.toList());
                return collect;
            }
            if (type.equals("3")){
                List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getJxjl).reversed())
                        .collect(Collectors.toList());
                return collect;
            }
            if (type.equals("4")){
                List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getSrqr).reversed())
                        .collect(Collectors.toList());
                return collect;
            }
            if (type.equals("5")){
                List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getYszkjsk).reversed())
                        .collect(Collectors.toList());
                return collect;
            }
            if (type.equals("6")){
                List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getKpMoney).reversed())
                        .collect(Collectors.toList());
                return collect;
            }
            if (type.equals("7")){
                List<Account> collect = list.stream().sorted(Comparator.comparing(Account::getHtqdMoney).reversed())
                        .collect(Collectors.toList());
                return collect;
            }
        }
        return list;
    }
}
